/******************************************************************************
*          
This program passes data back and forth between a PC running hyperterminal and a
flash memory chip. The demoflexis running this program emulates a USB connection
as a serial com port.  The user follows these steps:

- insert a programmed demoflexis board to PC.  listen for 
the ping indicating a new USB device added
- check for the location of the new com device under start/control panel/system/hardware/devices
- use the location of the JMxx to start a hyperterminal connection, 9600 baud, no flow control
- push the small button on the demoflexis card and follow directions as they appear on hyperterm
regarding how to enter or read data from the macronix chip. 


A driver must be installed on the host PC called HC9S08JMxx.inf and is included in this project) to
run the CDC interface.
****************************************************************************
Functions
  - hw_init();       MCU Initialization for speed, power and system settings
  - SPI_Init();      SPI Module Init for SPI channel 1.  The SD card uses SPI 2
  - usb_cfg_init();  configure the USB and CDC
  - cdc_init();

main() cycles through a cdc_process state machine and the minicom function which manages the highlevel
communication with the terminal

    cdc_process();                      USB CMX CDC process 


            
    -Terminal_Send_String()   // send a string to the hyperterm
    -cdc_kbhit()      // =1 when a character has been entered on hyperterm =0 otherwise
    -cdc_getch();     //get a character from the hyperterm
    -Terminal_Send_Byte  //View the characters as typed
    -SPI_Send_byte // send and recieve bytes to/from the SPI port 2
    - SPI_recieve_byte	                              

                                                                             */ 
/*****************************************************************************/

/* Include Files */
#include <hidef.h>
#include <stdio.h>          
#include "usb.h"
#include "target.h"
#include "usb_cdc.h"
#include "FslTypes.h"
#include "Fat.h"
#include "SD.h"
//#include "FAT_Debug.h"

void get_address(char);
void Term_numchar_out(char);

 /* Global Variables */
UINT8 gau8Minicom[512];
extern UINT8 gu8IntFlags;
char numbuff[16];
/************************************************************************************************/
void main(void)
{
  int cdc_in;
  int uart_in; 
  volatile UINT8 u8Error;
  UINT8   *pu8DataPointer;
  UINT8   u8Selection;
  UINT16  u16Counter;
  UINT16  temp_Counter;
  char    add1=0,add2=0,add3=0;
  
/**************    Initializations  ***********************/  
  PTBPE_PTBPE5 = 1;                     // DEMOFLEXISJMSD board 
  PTBDD_PTBDD5 = _IN;                     // Initialize Button in 
  hw_init();                            /* MCU Initialization */
  SPI_Init();                           /* SPI Module Init */
  cdc_in=uart_in=0xff+1;                /* USB CMX Init */
  usb_cfg_init();
  cdc_init();
  sprintf(numbuff,"0123456789ABCDEF");
 
/**************    Wait for button push to start process ***********************/  

  while(PTBD_PTBD5);                    /* Wait until button clic */
   
  CDC_Send_String("\r\n\r\n\t  Macrinix chip reader /writer /eraser\r\n");  
 
  while(1)
  {
    cdc_process();                      /* USB CMX CDC process */
  

/***********************************************************************/
/************* Poll to select option.  Choice goes into u8Selection ****/   
     
     
      Terminal_Send_String((UINT8*)"\r\n 1-read address?  \r\n");
      Terminal_Send_String((UINT8*)"\r\n 2-write address?   \r\n");
      Terminal_Send_String((UINT8*)"\r\n 3-erase sector?   \r\n");
     
      while(!cdc_kbhit());
      u8Selection=cdc_getch();
      Terminal_Send_String("\n\r");    

/***********************************************************************/
/******************  Read at specified address  ***********************/   
      
 
     if(u8Selection=='1'){
  
    pu8DataPointer=gau8Minicom;
    get_address(*pu8DataPointer);
    
    
//* Read Sector at specified address 

   SPI_SS=ENABLE;
   SPI_Send_byte(0x06);
   SPI_SS=DISABLE;
     
     
    
 	  SPI_SS=ENABLE;


	     SPI_Send_byte(0x03);   //read instruction
	     SPI_Send_byte(*pu8DataPointer);
       SPI_Send_byte(*(pu8DataPointer+2));
       SPI_Send_byte(*(pu8DataPointer+4));
     
       for(u16Counter=0;u16Counter<512;u16Counter++){
    	
	     *pu8DataPointer=SPI_Receive_byte();
	     pu8DataPointer++;
       }
    
    SPI_SS=DISABLE;
  
//* print Sector at specified address  

    pu8DataPointer=gau8Minicom;
    for(u16Counter=0;u16Counter<512;u16Counter++){
   
       Term_numchar_out(*pu8DataPointer);
       
     /*  
       if(*pu8DataPointer==0xff)*pu8DataPointer='0';
       else *pu8DataPointer='*';
       Terminal_Send_Byte(*pu8DataPointer); 
      */
        pu8DataPointer++;
      
    } 
     }
      
 /***********************************************************************/
/******************  Write at specified address  ***********************/   
      
 
     if(u8Selection=='2'){
   
         pu8DataPointer=gau8Minicom;
         get_address(*pu8DataPointer);
         add1=*pu8DataPointer;
         add2=*(pu8DataPointer+2);
         add3=*(pu8DataPointer+4);
         Terminal_Send_String((UINT8*)"\r\n  Enter characters  ctrl[z] to quit   \n\r");
  
//********Enter characters into pointer array***********  
        u16Counter=0;
        do
        {
            while(!cdc_kbhit());
            *pu8DataPointer=cdc_getch();
            u16Counter++;
            Terminal_Send_Byte(*pu8DataPointer);
            cdc_process();
        }while(*(pu8DataPointer++)!=0x1A);              //ctrlz  to stop editing
//********Enter pointer array into Macronix chip***********        
       
        pu8DataPointer=gau8Minicom;   
       
      SPI_SS=ENABLE;
        SPI_Send_byte(0x06);
      SPI_SS=DISABLE;
     
      SPI_SS=ENABLE;


	     SPI_Send_byte(0x02);   //write instruction
	     SPI_Send_byte(add1);
       SPI_Send_byte(add2);
       SPI_Send_byte(add3);
       //SPI_Send_byte(0x9f);
     
       for(temp_Counter=0;temp_Counter<u16Counter;temp_Counter++){
    	
	  SPI_Send_byte(*pu8DataPointer);
	  pu8DataPointer++;   
       }
    SPI_SS=DISABLE;
      
      
     }
  /***********************************************************************/
/******************  Erase specified sector  ***********************/   
      
 
     if(u8Selection=='3'){
   
         pu8DataPointer=gau8Minicom;
         get_address(*pu8DataPointer);
         add1=*pu8DataPointer;
         add2=*(pu8DataPointer+2);
         add3=*(pu8DataPointer+4);
       
      SPI_SS=ENABLE;
        SPI_Send_byte(0x06);
      SPI_SS=DISABLE;
     
      SPI_SS=ENABLE;


	     SPI_Send_byte(0x20);   //erase sector instruction
	     SPI_Send_byte(add1);
       SPI_Send_byte(add2);
       SPI_Send_byte(add3);
       //SPI_Send_byte(0x9f);
     
       SPI_SS=DISABLE;
      
      
     }
 

/*********************************************************************************/  
  }
  
}


/**************Get address value**********************************    
 This routine will convert the 1st 6 text characters in gauminicom
 into hex values in the 1st three places in minicom.  Ex
 
 Before executing  gauminicom = 'F','E','4','5','1','2'
 After executing   gauminicom = 0xfe,0x45,0x12
 
 */
 void get_address(char *point){
     char count=0;
     point=gau8Minicom;
     
     Terminal_Send_String((UINT8*)"\r\n-Address number? format NNNNNN six hex digits 0-400000  use caps \r\n");

     do
    {
        while(!cdc_kbhit());
        *point=cdc_getch();
        point++;
        Terminal_Send_Byte((*(point-1)));   //View the characters as typed
        cdc_process();                              //?
    }while((*(point-1))!=13);   //check for carriage return

    Terminal_Send_String("\n\r");
    
      point=gau8Minicom;
 //****Convert the ascii entered data into numerical data stored in point0, point1 and point2***********************   
    for(count=0;count<6;count++){
   
    if((*point>='A')&& (*point<='F'))*point=*point-55;
    else if((*point>='0')&& (*point<='9'))*point=*point-48;
    point++;
    
    }
    
    point=gau8Minicom;
    
    *point=*point<<4;
    *point=*point|*(point+1);
    
    *(point+2)=*(point+2)<<4;
    *(point+2)=*(point+2)|*(point+3);
    
    *(point+4)=*(point+4)<<4;
    *(point+4)=*(point+4)|*(point+5);
    


 }
    
    
    

void Term_numchar_out(char ch){
     char tmp;
     tmp=ch;
     tmp=tmp>>4;
     Terminal_Send_Byte(numbuff[tmp]);
     ch=ch&0x0f;
     Terminal_Send_Byte(numbuff[ch]);
     Terminal_Send_Byte(' ');
     return;
}

    
   
    
   
   
